package org.gl.customview.search;

import java.util.Iterator;

import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Spinner;
import android.widget.Toast;

import org.gl.android.base.R;

public class MultiTypeSpinnerAutoComTextSearch {
	Spinner mSpinner;
	public View view;
	private AutoCompleteTextView mAutoCompleteTextView;
	SearchAssistDirctionary dirctionary;
	Context conntext;
	private String[] m;
	Search<?> mSearch;

	public MultiTypeSpinnerAutoComTextSearch(ViewGroup viewGroup,
			Context _conntext, SearchAssistDirctionary _dictionary,
			Search<?> mSearch) {
		LayoutInflater li = (LayoutInflater) _conntext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.conntext = _conntext;
		this.view = li.inflate(R.layout.search, null);
		viewGroup.addView(view, new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));
		this.dirctionary = _dictionary;
		this.mSearch = mSearch;
		initSerchSpinner();
		initAutoComText();
	}

	private void initSerchSpinner() {
		mSpinner = (Spinner) view.findViewById(R.id.search_spinner);
		if (dirctionary != null && dirctionary.size() > 0) {
			m = new String[dirctionary.size()];
			Iterator<String> mm = dirctionary.keySet().iterator();
			int i = 0;
			while (mm.hasNext()) {
				m[i] = mm.next();
				i++;
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(conntext,
					android.R.layout.simple_spinner_item, m);

			// 设置下拉列表的风格
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			// 将adapter 添加到spinner中
			mSpinner.setAdapter(adapter);

			// 添加事件Spinner事件监听
			mSpinner.setOnItemSelectedListener(new SpinnerSelectedListener());
			currentType = m[0];
			mSpinner.setSelection(0, true);
		} else {
			mSpinner.setVisibility(View.GONE);
		}

	}

	private String currentType;
	private String[] currentDirc;
	private ArrayAdapter<String> av;

	class SpinnerSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			currentType = m[arg2];
			currentDirc = dirctionary.get(currentType);
			av = new ArrayAdapter<String>(conntext,
					android.R.layout.simple_dropdown_item_1line, currentDirc);
			mAutoCompleteTextView.setAdapter(av);
			Toast.makeText(conntext, m[arg2], Toast.LENGTH_SHORT).show();
		}

		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	Handler handler = new Handler();

	private void initAutoComText() {
		mAutoCompleteTextView = (AutoCompleteTextView) view
				.findViewById(R.id.search_autoCompleteTextView1);
		if (dirctionary != null && dirctionary.size() > 0) {
			currentDirc = dirctionary.get(currentType);
			av = new ArrayAdapter<String>(conntext,
					android.R.layout.simple_dropdown_item_1line, currentDirc);
			mAutoCompleteTextView.setAdapter(av);
		}
		mAutoCompleteTextView.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(final Editable s) {
				if (mSearch != null) {
					handler.removeCallbacksAndMessages(null);
					handler.postDelayed(new Runnable() {
						@Override
						public void run() {
							mSearch.search(currentType.hashCode(), s.toString());
						}
					}, 2000);

				}
			}
		});
	}

}
